package com.jpro.module.bpm.api.task.rpc;

import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.common.pojo.PageParam;
import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.common.util.number.NumberUtils;
import com.jpro.module.bpm.api.task.vo.BpmTaskApproveReqVO;
import com.jpro.module.bpm.api.task.vo.BpmTaskRejectReqVO;
import com.jpro.module.bpm.controller.admin.task.vo.task.BpmTaskPageReqVO;
import com.jpro.module.bpm.dal.mysql.definition.BpmProcessDefinitionInfoMapper;
import com.jpro.module.bpm.enums.task.BpmProcessInstanceStatusEnum;
import com.jpro.module.bpm.framework.flowable.core.util.FlowableUtils;
import com.jpro.module.bpm.service.definition.BpmProcessDefinitionService;
import com.jpro.module.bpm.service.task.BpmProcessInstanceService;
import com.jpro.module.bpm.service.task.BpmTaskService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static com.jpro.framework.common.pojo.CommonResult.success;
import static com.jpro.framework.web.core.util.WebFrameworkUtils.getLoginUserId;
import static com.jpro.module.bpm.api.task.enums.ApiConstants.BPM_FORMVARIABLES_CONTENTID;

/**
 * Flowable 任务 Api 实现类
 *
 * @author jprocms
 */
@RestController // 提供 RESTful API 接口，给 Feign 调用

@Validated
public class BpmTaskApiImpl implements BpmTaskApi {

    @Resource
    private BpmTaskService taskService;
    @Resource
    private BpmProcessInstanceService processInstanceService;
    @Resource
    private BpmProcessDefinitionService processDefinitionService;

    @Resource
    private BpmProcessDefinitionInfoMapper processDefinitionMapper;

    @Override
    public CommonResult<Boolean> approveTask(@RequestBody BpmTaskApproveReqVO reqVO) {
        taskService.approveTask(getLoginUserId(), reqVO);
        return success(true);
    }

    @Override
    public CommonResult<Boolean> rejectTask(@RequestBody BpmTaskRejectReqVO reqVO) {
        taskService.rejectTask(getLoginUserId(), reqVO);
        return success(true);
    }

    @Override
    public CommonResult<PageResult<Long>> getTodoContentIdPage(PageParam pageVO) {
        BpmTaskPageReqVO reqVO = new BpmTaskPageReqVO();
        reqVO.setPageNo(pageVO.getPageNo());
        reqVO.setPageSize(pageVO.getPageSize());
        final PageResult<Task> todoTaskPage = taskService.getTaskTodoPage(getLoginUserId(), reqVO);
        if (todoTaskPage.getTotal() <= 0) {
            return success(new PageResult<>(Collections.emptyList(), 0L));
        }
        List<Long> contentIds = new ArrayList<>();
        todoTaskPage.getList().forEach(c -> {
            final ProcessInstance processInstance = processInstanceService.getProcessInstance(c.getProcessInstanceId());
            String processInstanceId = processInstance.getId();
            // 这里如果后面审核加入其它数据审核 需要保证其它业务数据 from变量不能重复
            HistoricProcessInstance historicProcessInstance = processInstanceService.getHistoricProcessInstance(processInstanceId);
            final Integer processInstanceStatus = FlowableUtils.getProcessInstanceStatus(historicProcessInstance);
            //审批中的才算待审批
            if (BpmProcessInstanceStatusEnum.RUNNING.getStatus().equals(processInstanceStatus)) {
                final Map<String, Object> formVariables = FlowableUtils.getProcessInstanceFormVariable(historicProcessInstance);
                Object contentObj = formVariables.get(BPM_FORMVARIABLES_CONTENTID);
                if (contentObj != null) {
                    contentIds.add(NumberUtils.parseLong(contentObj.toString()));
                }
            }
        });
        return success(new PageResult<>(contentIds, (long) contentIds.size()));
    }

}
